package danran.bfs;

/**
 * @Classname IslandPerimeter
 * @Description TODO
 * @Date 2021/9/21 16:37
 * @Created by ASUS
 */
public class IslandPerimeter {
    int[][] dirs = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

    public int islandPerimeter(int[][] grid) {
        int cnt = 0;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                if (grid[i][j] == 1) {
                    for (int[] dir : dirs) {
                        int x = i + dir[0], y = j + dir[1];
                        if (x < 0 || x >= grid.length || y < 0 || y >= grid[0].length) {
                            cnt++;
                            continue;
                        }
                        if (grid[x][y] == 0) cnt++;
                    }
                }
            }
        }
        return cnt;
    }

    public static void main(String[] args) {
        String s = "[[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]".replace('[','{').replace(']','}');
        System.out.println(s);
        System.out.println(new IslandPerimeter().islandPerimeter(new int[][]{{0, 1, 0, 0}, {1, 1, 1, 0}, {0, 1, 0, 0}, {1, 1, 0, 0}}));
    }
}
